// pages/health/health.js
const app = getApp();
var util =  require('../../utils/util.js');
var json =  require('../../data/json.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentExperts: 0,
    currentSwiper: 0,
    iconNavList: json.iconNavList,
    isAdvertising: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    util.setNavBarColor("#ffffff",app.globalData.themeColor);
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      })
      //获取健康社区banner
      util.getAjax(app.globalData.apiUrl +'/get_community_health_banner',that.getBanner,{community_id: that.data.communityId})
      //获取今日精选商品
      util.getAjax(app.globalData.apiUrl +'/get_community_health_shop_product',that.getSelect,{community_id: that.data.communityId})
      //获取推荐专家
      util.getAjax(app.globalData.apiUrl +'/get_community_health_expert',that.getExperts,{community_id: that.data.communityId})
      //获取精选内容
      util.getAjax(app.globalData.apiUrl +'/get_community_health_new_video',that.getContent,{community_id: that.data.communityId})
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          that.setData({
            communityId: communityId
          })
          //获取健康社区banner
          util.getAjax(app.globalData.apiUrl +'/get_community_health_banner',that.getBanner,{community_id: communityId})
          //获取今日精选商品
          util.getAjax(app.globalData.apiUrl +'/get_community_health_shop_product',that.getSelect,{community_id: communityId})
          //获取推荐专家
          util.getAjax(app.globalData.apiUrl +'/get_community_health_expert',that.getExperts,{community_id: communityId})
          //获取精选内容
          util.getAjax(app.globalData.apiUrl +'/get_community_health_new_video',that.getContent,{community_id: communityId})
        }
      }
    }
  },

  //获取banner
  getBanner(res){
    console.log(res)
    var that = this
      console.log(res);
      if(res.data.length != 0){
      var bannerImgList = []
      for(var i in res.data){
        bannerImgList.push(res.data[i])
      }
      that.setData({
        isBanner: true,
        bannerImgList: bannerImgList,
        currentSwiper: 0
      })
    }
  },

  //获取精选
  getSelect(res){
    // console.log(res)
    var that = this
    if(res.data.length != 0){
      var goodsList = []
      for(var i in res.data){
        goodsList.push(res.data[i])
      }
      that.setData({
        isGoods: true,
        goodsList: goodsList
      })
    }
  },

  //获取专家
  getExperts(res){
    // console.log(res)
    var that = this
    if(res.data.length != 0){
      that.setData({
        isExperts: true,
        expertsList: res.data
      })
    }
  },

  //获取精选内容
  getContent(res){
    console.log(res)
    var that = this
    if(res.data.length != 0){
      var contentList = []
      for(var i in res.data){
        contentList.push(res.data[i])
      }
      that.setData({
        isContent: true,
        contentList: contentList
      })
    }
  },

  swiperChange:function(e){
    this.setData({
      currentSwiper: e.detail.current
    })
  },

  swiperExpertsChange:function(e){
    this.setData({
      currentExperts: e.detail.current
    })
  },

  imgBtn:function(e){
    var list = e.currentTarget.dataset.list
    var index = e.currentTarget.dataset.index
    if(list[index].url_id){
      switch (list[index].url_id) {
        case '1':
          wx.navigateTo({
            url: '/subPages/health/healthShop/healthShop',
          })
          break;
        case '2':
          wx.navigateTo({
            url: '/subPages/health/healthAdvice/healthAdvice',
          })
          break;
        case '3':
          wx.navigateTo({
            url: '/subPages/health/healthData/index/index',
          })
          break;
        case '4':
          wx.navigateTo({
            url: '/subPages/health/healthEducation/healthEducation',
          })
          break;
      }
    }
  },

  //导航按钮
  iconNavBtn:function(e){
    // console.log(e)
    var that = this
    var index = e.currentTarget.dataset.index
    switch (index+1) {
      case 1:
        wx.navigateTo({
          url: '/subPages/health/healthShop/healthShop',
        })
        break;
      case 2:
        wx.navigateTo({
          url: '/subPages/health/healthAdvice/healthAdvice',
        })
        break;
      case 3:
        if(app.globalData.userId){
          wx.navigateTo({
            url: '/subPages/health/healthData/index/index',
          })
        }else{
          util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
            if (app.globalData.userInfo) {
              //已授权
              wx.redirectTo({
                url: '/pages/login/login'
              })
            }else{
               // 在没有 open-type=getUserInfo 版本的兼容处理
              if(app.globalData.isAuthorized){
                app.userInfoReadyCallback = res => {
                  if (res) {
                    //已授权
                    wx.redirectTo({
                      url: '/pages/login/login'
                    })
                  }else{
                    //未授权
                    wx.navigateTo({
                      url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                    })
                  }
                }
              }else{
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          })
        }
        break;
      case 4:
        wx.navigateTo({
          url: '/subPages/health/healthEducation/healthEducation',
        })
        break;
    }

  },

  //点击食品
  clickGoods:function(e){
    if(app.globalData.userId){
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/subPages/health/healthShopDetails/healthShopDetails?id=' + id,
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //专家按钮
  expertsBtn:function(e){
    if(app.globalData.userId){
      var item = e.currentTarget.dataset.item
      wx.navigateTo({
        url: '/subPages/health/consult/consult?type=' + 1 + '&item=' + JSON.stringify(item),
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //精选内容详情
  contentBtn:function(e){
    if(e.currentTarget.dataset.type == 1){
      wx.navigateTo({
        url: '/subPages/health/article/article?id=' + e.currentTarget.dataset.id,
      })
    }
  },

  //播放结束
  viedoEnd:function(e){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/add_see',function(res){
      //获取精选内容
      util.getAjax(app.globalData.apiUrl +'/get_community_health_new_video',that.getContent,{community_id: that.data.communityId})
    },{id: e.currentTarget.dataset.id,type: 2})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    if(that.data.communityId){
      //获取健康社区banner
      util.getAjax(app.globalData.apiUrl +'/get_community_health_banner',that.getBanner,{community_id: that.data.communityId})
      //获取今日精选商品
      util.getAjax(app.globalData.apiUrl +'/get_community_health_shop_product',that.getSelect,{community_id: that.data.communityId})
      //获取推荐专家
      util.getAjax(app.globalData.apiUrl +'/get_community_health_expert',that.getExperts,{community_id: that.data.communityId})
      //获取精选内容
      util.getAjax(app.globalData.apiUrl +'/get_community_health_new_video',that.getContent,{community_id: that.data.communityId})
    }
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
